/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.actscode.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.dto.FileInfoSaveDto;
import com.icinfo.cloud.provider.common.enums.AttachmentRequestContentTypeEnum;
import com.icinfo.cloud.provider.common.enums.CardTypeTransEnum;
import com.icinfo.cloud.provider.common.enums.FileTransferFormEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.oss.service.IOssOperateService;
import com.icinfo.cloud.provider.common.utils.ActCodeUtils;
import com.icinfo.cloud.provider.common.utils.SignatureUtil;
import com.icinfo.cloud.provider.punish.actscode.dto.ActsCodeDto;
import com.icinfo.cloud.provider.punish.actscode.dto.ActsCodeRevokeDto;
import com.icinfo.cloud.provider.punish.actscode.mapper.CaseActsCodeInfoMapper;
import com.icinfo.cloud.provider.punish.actscode.model.CaseActsCodeInfo;
import com.icinfo.cloud.provider.punish.actscode.service.ICaseActsCodeErrInfoService;
import com.icinfo.cloud.provider.punish.actscode.service.ICaseActsCodeInfoService;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.csource.manage.enums.CaseSourceEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartyTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesLegalInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailSimpleInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CasePartiesLegalInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainDetailSimpleInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseReasonMainRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CasePartiesRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseReasonMainRecordVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.DateUtils;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:  case_acts_code_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年11月08日
 */
@Service
public class CaseActsCodeInfoServiceImpl extends MyBatisServiceSupport implements ICaseActsCodeInfoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseActsCodeInfoServiceImpl.class);

    @Autowired
    private CaseActsCodeInfoMapper caseActsCodeInfoMapper;
    @Resource
    private ISysDepartParamService sysDepartParamService;
    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;
    @Resource
    private IOssOperateService iOssOperateService;
    @Resource
    private ICaseActsCodeErrInfoService caseActsCodeErrInfoService;
    @Value("${actionCode.generateUrl}")
    private String generateUrl;
    @Value("${actionCode.systemCode}")
    private String systemCode;
    @Value("${actionCode.actionTypeCode}")
    private String actionTypeCode;
    @Value("${actionCode.appKey}")
    private String appKey;
    @Value("${actionCode.appSecret}")
    private String appSecret;
    @Value("${actionCode.qrCodeContent}")
    private String qrCodeContent;
    @Value("${actionCode.queryUrl}")
    private String queryUrl;
    @Value("${actionCode.revokeUrl}")
    private String revokeUrl;
    @Value("${actionCode.checkConnectUrl}")
    private String checkConnectUrl;
    @Resource
    private ICasePartiesRecordService casePartiesRecordService;
    @Resource
    private CasePartiesLegalInfoMapper casePartiesLegalInfoMapper;
    @Resource
    private CaseMainDetailInfoMapper caseMainDetailInfoMapper;
    @Resource
    private ICaseReasonMainRecordService caseReasonMainRecordService;
    @Resource
    private ICaseMainDetailSimpleInfoService caseMainDetailSimpleInfoService;
    /**
     * 保存行政行为码信息
     * @author liyafeng
     * @date 2022/11/8
      * @param actsCodeDto
     * @return {@link int}
     */
    @Override
    public int saveCaseActsCodeInfo(ActsCodeDto actsCodeDto) {
        int num=0;
        CaseMainInfoVo caseMainInfoVo=caseMainInfoMapper.selectCaseMainInfo(actsCodeDto.getCaseId());
        String actionCode=genCaseActCode(actsCodeDto,caseMainInfoVo);
        if (StringUtils.isBlank(actionCode)) {
            throw new BusinessException("赋码生成接口赋码生成为空!");
        }
        System.out.println("赋码生成结束————————————————————————————————————————");
        //更新案件主表
        CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
        updateCaseMainInfo.setId(caseMainInfoVo.getId());
        updateCaseMainInfo.setCaseActsCode(actionCode);

        //生成二维码
        String qrUrl="";
        ByteArrayOutputStream bos =new ByteArrayOutputStream();
        try {
            ActCodeUtils.createQrCodeToOutputStream(qrCodeContent+actionCode,bos);
            FileInfoSaveDto fileInfo = new FileInfoSaveDto();
            fileInfo.setFileBytes(bos.toByteArray());
            fileInfo.setFileTransferForm(FileTransferFormEnum.FILE_TRANSFER_FORM_3.getCode());
            fileInfo.setFileType(AttachmentRequestContentTypeEnum.png.getCode());
            Result result = iOssOperateService.uploadFile(fileInfo);
            qrUrl=result.getData().toString();
        }catch (Exception e){
            throw new BusinessException("生成二维码异常!");
        }finally {
            try {
                bos.close();
            }catch (Exception e){
                throw new BusinessException("生成二维码异常!");
            }
        }
        //生成条形码
        String barUrl="";
        ByteArrayOutputStream barBos =new ByteArrayOutputStream();
        try {
            ActCodeUtils.generateBarCode(actionCode,barBos);
            FileInfoSaveDto fileInfo = new FileInfoSaveDto();
            fileInfo.setFileBytes(barBos.toByteArray());
            fileInfo.setFileTransferForm(FileTransferFormEnum.FILE_TRANSFER_FORM_3.getCode());
            fileInfo.setFileType(AttachmentRequestContentTypeEnum.png.getCode());
            Result barResult = iOssOperateService.uploadFile(fileInfo);
            barUrl=barResult.getData().toString();
        }catch (Exception e){
            throw new BusinessException("生成二维码异常!");
        }finally {
            try {
                barBos.close();
            }catch (Exception e){
                throw new BusinessException("生成二维码异常!");
            }
        }
        updateCaseMainInfo.setCaseQrCodeUrl(qrUrl);
        updateCaseMainInfo.setCaseBarCodeUrl(barUrl);
        caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
        //保存
        CaseActsCodeInfo caseActsCodeInfo=new CaseActsCodeInfo();
        caseActsCodeInfo.setId(StringUtils.uuid());
        caseActsCodeInfo.setCaseActsCode(actionCode);
        caseActsCodeInfo.setCaseId(caseMainInfoVo.getId());
        caseActsCodeInfo.setBarCodeUrl(barUrl);
        caseActsCodeInfo.setQrCodeUrl(qrUrl);
        num=caseActsCodeInfoMapper.insert(caseActsCodeInfo);
        //回调检查接口
        if(StringUtils.isNotBlank(caseMainInfoVo.getCaseExternalActsCode())){
            doReCallcheckConnect(caseMainInfoVo.getCaseExternalActsCode());
        }
        return num;
    }
    /**
     * 生成行政行为码
     * @author liyafeng
     * @date 2023/3/21
      * @param actsCodeDto
     * @return {@link String}
     */
    private String genCaseActCode(ActsCodeDto actsCodeDto,CaseMainInfoVo caseMainInfoVo) {
        String caseSourceId="";
        String caseHappenTime="";
        String transferDept="";
        String caseAddressName="";
        String transferDeptUniscid="";
        String caseAddressCode="";
        String punishType=caseMainInfoVo.getCaseType();
        String caseType=caseMainInfoVo.getCaseType();
        if(Constant.STRING_1.equals(caseType)){
            punishType=Constant.STRING_2;
        }else if(Constant.STRING_2.equals(caseType)){
            punishType=Constant.STRING_1;
        }
        if(Constant.STRING_1.equals(caseType)){
            CaseMainDetailSimpleInfo caseMainDetailSimpleInfo = caseMainDetailSimpleInfoService.getCaseMainDetailSimpleInfo(actsCodeDto.getCaseId());
            caseSourceId= "1";
            caseHappenTime=caseMainDetailSimpleInfo.getCaseHappenTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            transferDept="";
            caseAddressName=caseMainDetailSimpleInfo.getCaseHappenAddressDetail();
            transferDeptUniscid="";
            caseAddressCode="";
            //punishType=caseMainDetailSimpleInfo.getPunishKinds();
        }else{
            CaseMainDetailInfo caseMainDetailInfo=caseMainDetailInfoMapper.selectByPrimaryKey(actsCodeDto.getCaseId());
            caseSourceId= caseMainDetailInfo.getCaseSourceId();
            caseHappenTime=caseMainDetailInfo.getCaseHappenTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            transferDept=caseMainDetailInfo.getTransferUnit();
            caseAddressName=caseMainDetailInfo.getCaseHappenAddressDetailFull();
            transferDeptUniscid=caseMainDetailInfo.getTransferUnit();
            caseAddressCode=caseMainDetailInfo.getCaseHappenAddressAreaCode();
            //punishType=caseMainDetailInfo.getPunishKinds();
        }
        List<CaseReasonMainRecordVo> caseReasonMainRecordVos=caseReasonMainRecordService.getCaseReasonRecordListByCaseId(actsCodeDto.getCaseId());
        List<CasePartiesRecordVo> casePartiesRecordVos=casePartiesRecordService.getCasePartiesInfo(actsCodeDto.getCaseId());
        CasePartiesRecordVo casePartiesRecordVo=new CasePartiesRecordVo();
        if(!ObjectUtils.isEmpty(casePartiesRecordVos)){
            casePartiesRecordVo=casePartiesRecordVos.get(0);
        }
        String actionCode="";
        //调用赋码接口
        Map<String,Object> json = new HashMap<>();
        json.put("systemCode", systemCode);
        json.put("businessUnique", caseMainInfoVo.getId());
        //根据orgcode 获取 统一信用代码
        SysDepartParam sysDepartParam=sysDepartParamService.getSysDepartParamByOrgCode(caseMainInfoVo.getCaseOrgCode(),caseMainInfoVo.getCaseDeptCode());
        if(null==sysDepartParam){
            throw new BusinessException("未查到统一信用代码信息");
        }
        json.put("uniscid",sysDepartParam.getUnicode());
        json.put("actionTypeCode", "03");
        json.put("appKey", appKey);
        String customCode=getCustomCode(actsCodeDto.getCaseId(),caseMainInfoVo.getCaseType(),caseSourceId,casePartiesRecordVos);
        json.put("customCode", customCode);
        if(StringUtils.isNotBlank(caseMainInfoVo.getCaseExternalActsCode())){
            json.put("prevActionCode", caseMainInfoVo.getCaseExternalActsCode());
        }
        long currentTimeMillis = System.currentTimeMillis();
        //String canonicalString = currentTimeMillis + "\n" + "f63d579fc2f5dee76b2302faf2ab3737";
        json.put("timestamp", currentTimeMillis);
        String canonicalString = currentTimeMillis + "\n" + "41bce5eaf23174026b2302faf2ab3737";
        String signValue=SignatureUtil.computeSignature("41bce5eaf23174026b2302faf2ab3737",canonicalString);
        json.put("signValue", signValue);
        Map<String,Object> punishInfoMap=new HashMap<>();
        String regulatoryMattersName="";
        String regulatoryMattersCode="";
        if(!ObjectUtils.isEmpty(caseReasonMainRecordVos)) {
            regulatoryMattersName=caseReasonMainRecordVos.get(0).getCaseReasonName();
            regulatoryMattersCode=caseReasonMainRecordVos.get(0).getCaseReasonCode();
            /*for (CaseReasonMainRecordVo caseReasonMainRecordVo : caseReasonMainRecordVos) {

            }*/
        }
        punishInfoMap.put("regulatoryMattersName",regulatoryMattersName);
        punishInfoMap.put("deptName",caseMainInfoVo.getCaseOrgFullName());
        punishInfoMap.put("personConcernedType",customCode.substring(0,1));
        punishInfoMap.put("caseDate",caseHappenTime);
        String approvalDate=DateUtils.formatDate(new Date(),"yyyy-MM-dd");
        String dateStr=DateUtils.formatDate(new Date(),"yyyyMMdd");
        if(null!=caseMainInfoVo.getCaseRegistTime()){
            approvalDate=caseMainInfoVo.getCaseRegistTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            dateStr=caseMainInfoVo.getCaseRegistTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        }
        //caseMainInfo.getCaseRegistTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        json.put("dateStr", dateStr);
        punishInfoMap.put("approvalDate",approvalDate);
        punishInfoMap.put("transferDept",transferDept);
        punishInfoMap.put("personConcernedCardNum",casePartiesRecordVo.getPartyIdentityCode());
        punishInfoMap.put("regulatoryMattersCode",regulatoryMattersCode);
        punishInfoMap.put("caseAddressName",caseAddressName);
        punishInfoMap.put("transferDeptUniscid",transferDeptUniscid);
        String personConcernedCardType=getPersonConcernedCardType(casePartiesRecordVo.getPartyType(),casePartiesRecordVo.getPartyIdentityType());
        punishInfoMap.put("personConcernedCardType",personConcernedCardType);
        punishInfoMap.put("caseAddressCode",caseAddressCode);
        punishInfoMap.put("caseDetailAddress",caseAddressName);
        punishInfoMap.put("personConcernedName",casePartiesRecordVo.getPartyName());
        punishInfoMap.put("punishType",punishType);
        punishInfoMap.put("caseSource",getThreeNumByCaseSource(caseSourceId));
        punishInfoMap.put("orgCode",caseMainInfoVo.getCaseOrgCode());



        json.put("punishInfoReq",punishInfoMap);
        String param = JSON.toJSONString(json);
        System.out.println("赋码生成开始————————————————————————————————————————");
        System.out.println("param:+++"+param);
        String response = HttpUtil.post(generateUrl, param);
        System.out.println("赋码返回：+++++++"+response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getIntValue("code") == 200) {
            JSONObject dataJsonObject = jsonObject.getJSONObject("data");
            actionCode = dataJsonObject.getString("actionCode");
        }else if(jsonObject.getIntValue("code") == 500){
            //已经生成调用查询接口
            String message=jsonObject.getString("message");
            if(message.contains("行政行为码已生成")){
                Map<String,Object> paramMap = new HashMap<>();
                paramMap.put("businessUnique",caseMainInfoVo.getId());
                paramMap.put("systemCode", systemCode);
                long now = System.currentTimeMillis();
                //String canonicalString = currentTimeMillis + "\n" + "f63d579fc2f5dee76b2302faf2ab3737";
                paramMap.put("timestamp", now);
                String canonicalStringNow = now + "\n" + "41bce5eaf23174026b2302faf2ab3737";
                String signValueNow=SignatureUtil.computeSignature("41bce5eaf23174026b2302faf2ab3737",canonicalStringNow);
                paramMap.put("signValue", signValueNow);
                paramMap.put("status",Constant.STRING_1);
                paramMap.put("appKey", appKey);
                String paramMapJson=JSON.toJSONString(paramMap);
                String result = HttpUtil.post(queryUrl, paramMapJson);
                System.out.println("行政行为码已生成，查码返回参数：+++++"+result);
                JSONObject resultObject = JSONObject.parseObject(result);
                if(resultObject.getIntValue("code") == 200){
                    JSONArray array = resultObject.getJSONArray("data");
                    JSONObject resultObjectJSONObject =array.getJSONObject(0);
                    actionCode = resultObjectJSONObject.getString("actionCode");
                }else{
                    JSONArray errorJSONArray = jsonObject.getJSONArray("errors");
                    throw new BusinessException("赋码生成接口接口调用失败!" + "失败原因：" + errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
                }
                //行政行为码已生成  注销重新生成
                //注销
                revokeActCode(actionCode,caseMainInfoVo.getId());
                System.out.println("赋码重新生成开始————————————————————————————————————————");
                System.out.println("param:+++"+param);
                response = HttpUtil.post(generateUrl, param);
                JSONObject reJsonObject = JSONObject.parseObject(response);
                if (reJsonObject.getIntValue("code") == 200) {
                    JSONObject dataJsonObject = reJsonObject.getJSONObject("data");
                    actionCode = dataJsonObject.getString("actionCode");
                }else{
                    JSONArray errorJSONArray = reJsonObject.getJSONArray("errors");
                    caseActsCodeErrInfoService.saveCaseActsCodeErrInfo(actsCodeDto.getCaseId(),errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
                    throw new BusinessException("赋码生成接口接口调用失败!" + "失败原因：" + errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
                }

            }else{
                JSONArray errorJSONArray = jsonObject.getJSONArray("errors");
                caseActsCodeErrInfoService.saveCaseActsCodeErrInfo(actsCodeDto.getCaseId(),errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
                throw new BusinessException("赋码生成接口接口调用失败!" + "失败原因：" + errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
            }
        } else {
            JSONArray errorJSONArray = jsonObject.getJSONArray("errors");
            caseActsCodeErrInfoService.saveCaseActsCodeErrInfo(actsCodeDto.getCaseId(),errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
            throw new BusinessException("赋码生成接口接口调用失败!" + "失败原因：" + errorJSONArray.getJSONObject(0).getString("field") + errorJSONArray.getJSONObject(0).getString("info"));
        }
        return actionCode;
    }
    /**
     * 注销
     * @author liyafeng
     * @date 2023/3/21
      * @param actionCode
     * @param caseId
     * @return
     */
    private void revokeActCode(String actionCode, String caseId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("actionCode", actionCode);
        paramMap.put("businessUnique", caseId);
        paramMap.put("systemCode", systemCode);
        paramMap.put("appKey", appKey);
        long now = System.currentTimeMillis();
        //String canonicalString = currentTimeMillis + "\n" + "f63d579fc2f5dee76b2302faf2ab3737";
        paramMap.put("timestamp", now);
        String canonicalStringNow = now + "\n" + "41bce5eaf23174026b2302faf2ab3737";
        String signValueNow = SignatureUtil.computeSignature("41bce5eaf23174026b2302faf2ab3737", canonicalStringNow);
        paramMap.put("signValue", signValueNow);
        String paramMapJson = JSON.toJSONString(paramMap);
        String result = HttpUtil.post(revokeUrl, paramMapJson);
        System.out.println("注销结果：——————————————" + result);

    }

    /**
     *调检查接口
     * @author liyafeng
     * @date 2023/1/31
      * @param caseExternalActsCode
     * @return
     */
    private void doReCallcheckConnect(String caseExternalActsCode) {
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("actionCode",caseExternalActsCode);
            String mapJson=JSON.toJSONString(map);
            HttpUtil.post(checkConnectUrl, mapJson);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取转化后的当事人类型编码
     * @author liyafeng
     * @date 2022/11/12
      * @param partyType
     * @param partyIdentityType
     * @return {@link String}
     */
    private String getPersonConcernedCardType(String partyType, String partyIdentityType) {
        String personConcernedCardType="";
        //如果是法人 写死统一信用代码
        if(Constant.STRING_2.equals(partyType)){
            personConcernedCardType=Constant.STRING_1;
        }else{
            //如果是个人 转化类型
            personConcernedCardType= CardTypeTransEnum.getValueByCode(partyIdentityType);
        }
        return personConcernedCardType;
    }

    /**
     * 获取自定义码
     * @author liyafeng
     * @date 2022/11/10
      * @param caseId
     * @return {@link String}
     */
    private String getCustomCode(String caseId,String caseType,String caseSource,List<CasePartiesRecordVo> casePartiesRecordVos) {
        String customCode="";
        String oneNum="";
        String twoNum="";
        String threeNum="";
        //例如1表示自然人，2表示个体工商户；第二位表示行政处罚程序的类型，例如1表示普通程序，2表示简易程序；第三位表示案件来源，例如1表示日常巡查，2表示部门移交；第四位、第五位（建议为非现场执法）为预留码；第六位为校验码
        if(!ObjectUtils.isEmpty(casePartiesRecordVos)) {
            if (casePartiesRecordVos.size()==1) {
                String partyType=casePartiesRecordVos.get(0).getPartyType();
                String partyId=casePartiesRecordVos.get(0).getPartyId();
                if(PartyTypeEnum.PERSON.getCode().equals(partyType)){
                    oneNum=Constant.STRING_1;
                }else{
                    CasePartiesLegalInfo casePartiesLegalInfo=casePartiesLegalInfoMapper.selectByPrimaryKey(partyId);
                    String entType=casePartiesLegalInfo.getEntType();
                    if(Constant.STRING_1.equals(entType)){
                        oneNum=Constant.STRING_3;
                    }else if(Constant.STRING_2.equals(entType)){
                        oneNum=Constant.STRING_2;
                    }else{
                        oneNum=Constant.STRING_4;
                    }
                }
            }else{
                oneNum=Constant.STRING_5;
            }
        }
        if(Constant.STRING_1.equals(caseType)){
            twoNum=Constant.STRING_2;
        }else{
            twoNum=Constant.STRING_1;
        }
        threeNum=getThreeNumByCaseSource(caseSource);
        customCode=oneNum+twoNum+threeNum+"00";
        return customCode;
    }
    /**
     * 转化案件来源类型
     * @author liyafeng
     * @date 2023/3/13
      * @param caseSource
     * @return {@link String}
     */
    private String getThreeNumByCaseSource(String caseSource) {
        String threeNum="";
        if(CaseSourceEnum.CASE_SOURCE_SHJB.getCode().equals(caseSource)){
            threeNum=Constant.STRING_3;
        }else if(CaseSourceEnum.CASE_SOURCE_YGBMYS.getCode().equals(caseSource)){
            threeNum=Constant.STRING_2;
        }else if(CaseSourceEnum.CASE_SOURCE_QT.getCode().equals(caseSource)){
            threeNum=Constant.STRING_9;
        }else{
            threeNum=caseSource;
        }
        return threeNum;
    }

    /**
     * 根据caseId查询
     * @author liyafeng
     * @date 2022/11/9
      * @param caseId
     * @return {@link CaseActsCodeInfo}
     */
    @Override
    public CaseActsCodeInfo getCaseActsCodeInfo(String caseId) {
        Example example=new Example(CaseActsCodeInfo.class);
        example.createCriteria().andEqualTo("caseId",caseId);
        List<CaseActsCodeInfo> caseActsCodeInfos= caseActsCodeInfoMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(caseActsCodeInfos)){
            return caseActsCodeInfos.get(Constant.INTEGER_0);
        }
        return null;
    }
    /**
     * 行政行为码注销
     * @author liyafeng
     * @date 2023/1/13
      * @param actsCodeRevokeDto
     * @return {@link int}
     */
    @Override
    public int revoke(ActsCodeRevokeDto actsCodeRevokeDto) {
       List<ActsCodeRevokeDto>  actsCodeRevokeDtos=caseActsCodeInfoMapper.selectActsCodeRevokeDtoList();
        for (ActsCodeRevokeDto dto:actsCodeRevokeDtos) {
       /* CaseMainInfo caseMainInfo=caseMainInfoMapper.selectByPrimaryKey(actsCodeRevokeDto.getBusinessUnique());
        if(ObjectUtils.isEmpty(caseMainInfo)){
            return 0;
        }*/
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("actionCode", dto.getActionCode());
            paramMap.put("businessUnique", dto.getBusinessUnique());
            paramMap.put("systemCode", systemCode);
            paramMap.put("appKey", appKey);
            long now = System.currentTimeMillis();
            //String canonicalString = currentTimeMillis + "\n" + "f63d579fc2f5dee76b2302faf2ab3737";
            paramMap.put("timestamp", now);
            String canonicalStringNow = now + "\n" + "41bce5eaf23174026b2302faf2ab3737";
            String signValueNow = SignatureUtil.computeSignature("41bce5eaf23174026b2302faf2ab3737", canonicalStringNow);
            paramMap.put("signValue", signValueNow);
            String paramMapJson = JSON.toJSONString(paramMap);
            String result = HttpUtil.post(revokeUrl, paramMapJson);
            System.out.println("注销结果：——————————————" + result);
        /*JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getIntValue("code") == 200) {
            //回调检查接口
            if(StringUtils.isNotBlank(caseMainInfo.getCaseExternalActsCode())){
                doReCallcheckConnect(caseMainInfo.getCaseExternalActsCode());
            }
            //更新案件主表行政行为码信息
            CaseMainInfo updateCaseMainInfo=new CaseMainInfo();
            updateCaseMainInfo.setId(actsCodeRevokeDto.getBusinessUnique());
            updateCaseMainInfo.setCaseBarCodeUrl("");
            updateCaseMainInfo.setCaseQrCodeUrl("");
            updateCaseMainInfo.setCaseActsCode("");
            caseMainInfoMapper.updateByPrimaryKeySelective(updateCaseMainInfo);
        }*/
            /*}*/
        }
        return 1;
    }
}